/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package gui;

import interfaces.EditPanel;

import java.awt.Dimension;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import tools.InputType;
import tools.StringTools;
import database.DBConnectionFactory;

/**
 * Settings panel for races
 * 
 * 
 */
public class RacesPanel extends JPanel implements EditPanel {
   private static final long serialVersionUID = 4228896307757033356L;
   private JTextField name;
   private JTextField shortname;
   private JTextField ospecname;
   private JTextField ospecstr;
   private JTextField dspecname;
   private JTextField dspecstr;
   private JTextField elitename;
   private JTextField eliteostr;
   private JTextField elitedstr;
   private JTextField eliteoutratio;
   private JTextField elitenw;
   private String itemName;
   private final DialogPanel parent;

   public RacesPanel(final DialogPanel parent) {
	  super();
	  this.parent = parent;
	  initialize();
   }

   @Override
   public void checkInputs() {
	  if (name.getText().equals("") || shortname.getText().equals("") || ospecname.getText().equals("")
			|| !StringTools.validateInput(InputType.INT, ospecstr.getText()) || dspecname.getText().equals("")
			|| !StringTools.validateInput(InputType.INT, dspecstr.getText()) || elitename.getText().equals("")
			|| !StringTools.validateInput(InputType.INT, eliteostr.getText())
			|| !StringTools.validateInput(InputType.INT, elitedstr.getText())
			|| !StringTools.validateInput(InputType.INT, eliteoutratio.getText())
			|| !StringTools.validateInput(InputType.DOUBLE, elitenw.getText())) {
		 parent.setSaveButtonEnabled(false);
	  } else {
		 parent.setSaveButtonEnabled(true);
	  }
   }

   @Override
   public boolean delete() {
	  if (name.getText().equals(""))
		 return false;
	  PreparedStatement ps = null;
	  boolean success = false;
	  try {
		 ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("DELETE FROM race WHERE name = ?");
		 ps.setString(1, itemName);
		 ps.executeUpdate();
		 success = true;
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return success;
   }

   @Override
   public String getItemName() {
	  return itemName;
   }

   /**
    * Initializes the components
    */
   private void initialize() {
	  setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
	  name = new JTextField();
	  name.setPreferredSize(new Dimension(300, 20));
	  name.getDocument().addDocumentListener(new MyDocumentListener(this));
	  shortname = new JTextField();
	  shortname.setPreferredSize(new Dimension(300, 20));
	  shortname.getDocument().addDocumentListener(new MyDocumentListener(this));
	  ospecname = new JTextField();
	  ospecname.setPreferredSize(new Dimension(300, 20));
	  ospecname.getDocument().addDocumentListener(new MyDocumentListener(this));
	  ospecstr = new JTextField();
	  ospecstr.setPreferredSize(new Dimension(300, 20));
	  ospecstr.getDocument().addDocumentListener(new MyDocumentListener(this));
	  dspecname = new JTextField();
	  dspecname.setPreferredSize(new Dimension(300, 20));
	  dspecname.getDocument().addDocumentListener(new MyDocumentListener(this));
	  dspecstr = new JTextField();
	  dspecstr.setPreferredSize(new Dimension(300, 20));
	  dspecstr.getDocument().addDocumentListener(new MyDocumentListener(this));
	  elitename = new JTextField();
	  elitename.setPreferredSize(new Dimension(300, 20));
	  elitename.getDocument().addDocumentListener(new MyDocumentListener(this));
	  eliteostr = new JTextField();
	  eliteostr.setPreferredSize(new Dimension(300, 20));
	  eliteostr.getDocument().addDocumentListener(new MyDocumentListener(this));
	  elitedstr = new JTextField();
	  elitedstr.setPreferredSize(new Dimension(300, 20));
	  elitedstr.getDocument().addDocumentListener(new MyDocumentListener(this));
	  eliteoutratio = new JTextField();
	  eliteoutratio.setPreferredSize(new Dimension(300, 20));
	  eliteoutratio.getDocument().addDocumentListener(new MyDocumentListener(this));
	  elitenw = new JTextField();
	  elitenw.setPreferredSize(new Dimension(300, 20));
	  elitenw.getDocument().addDocumentListener(new MyDocumentListener(this));

	  this.add(new CustomPanel(new CustomLabel("Name", 120, 20), name));
	  this.add(new CustomPanel(new CustomLabel("Short Name", 120, 20), shortname));
	  this.add(new CustomPanel(new CustomLabel("Ospec Name", 120, 20), ospecname));
	  this.add(new CustomPanel(new CustomLabel("Ospec Strength", 120, 20), ospecstr));
	  this.add(new CustomPanel(new CustomLabel("Dspec Name", 120, 20), dspecname));
	  this.add(new CustomPanel(new CustomLabel("Dspec Strength", 120, 20), dspecstr));
	  this.add(new CustomPanel(new CustomLabel("Elite Name", 120, 20), elitename));
	  this.add(new CustomPanel(new CustomLabel("Elite Off Strength", 120, 20), eliteostr));
	  this.add(new CustomPanel(new CustomLabel("Elite Def Strength", 120, 20), elitedstr));
	  this.add(new CustomPanel(new CustomLabel("Elite Out Ratio", 120, 20), eliteoutratio));
	  this.add(new CustomPanel(new CustomLabel("Elite NW", 120, 20), elitenw));
   }

   @Override
   public JPanel load(final String item) {
	  itemName = item;
	  removeAll();
	  validate();
	  initialize();
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  try {
		 ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("SELECT * FROM race WHERE name = ?");
		 ps.setString(1, item);
		 rs = ps.executeQuery();

		 if (rs.next()) {
			name.setText(rs.getString("name"));
			name.setEditable(false);
			shortname.setText(rs.getString("shortname"));
			ospecname.setText(rs.getString("ospecname"));
			ospecstr.setText(String.valueOf(rs.getInt("ospecstr")));
			dspecname.setText(rs.getString("dspecname"));
			dspecstr.setText(String.valueOf(rs.getInt("dspecstr")));
			elitename.setText(rs.getString("elitename"));
			eliteostr.setText(String.valueOf(rs.getInt("eliteostr")));
			elitedstr.setText(String.valueOf(rs.getInt("elitedstr")));
			eliteoutratio.setText(String.valueOf(rs.getInt("eliteoutratio")));
			elitenw.setText(String.valueOf(rs.getDouble("elitenw")));
		 } else {
			name.setText(item);
		 }
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			rs.close();
		 } catch (final Exception e) {
		 }
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return this;
   }

   @Override
   public boolean save() {
	  if (name.getText().equals("") || shortname.getText().equals("")
			|| !StringTools.validateInput(InputType.INT, ospecstr.getText()) || ospecname.getText().equals("")
			|| !StringTools.validateInput(InputType.INT, ospecstr.getText()) || dspecname.getText().equals("")
			|| !StringTools.validateInput(InputType.INT, dspecstr.getText()) || elitename.getText().equals("")
			|| !StringTools.validateInput(InputType.INT, eliteostr.getText())
			|| !StringTools.validateInput(InputType.INT, elitedstr.getText())
			|| !StringTools.validateInput(InputType.INT, eliteoutratio.getText())
			|| !StringTools.validateInput(InputType.DOUBLE, elitenw.getText()))
		 return false;
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  boolean success = false;
	  try {
		 ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("SELECT * FROM race WHERE name = ?");
		 ps.setString(1, itemName);
		 rs = ps.executeQuery();

		 if (rs.next()) {
			try {
			   ps.close();
			} catch (final Exception e) {
			}

			ps = DBConnectionFactory
				  .getConnectionHandler()
				  .getConnection()
				  .prepareStatement(
						"UPDATE race SET ospecname = ?,ospecstr = ?,dspecname = ?,"
							  + "dspecstr = ?,elitename = ?,eliteostr = ?,elitedstr = ?,eliteoutratio = ?, shortname = ?, elitenw = ? WHERE name = ?");
			ps.setString(1, ospecname.getText());
			ps.setInt(2, Integer.parseInt(ospecstr.getText()));
			ps.setString(3, dspecname.getText());
			ps.setInt(4, Integer.parseInt(dspecstr.getText()));
			ps.setString(5, elitename.getText());
			ps.setInt(6, Integer.parseInt(eliteostr.getText()));
			ps.setInt(7, Integer.parseInt(elitedstr.getText()));
			ps.setInt(8, Integer.parseInt(eliteoutratio.getText()));
			ps.setString(9, shortname.getText());
			ps.setDouble(10, Double.parseDouble(elitenw.getText()));
			ps.setString(11, name.getText());
			ps.executeUpdate();
		 } else {
			try {
			   ps.close();
			} catch (final Exception e) {
			}

			ps = DBConnectionFactory.getConnectionHandler().getConnection()
				  .prepareStatement("INSERT INTO race VALUES(?,?,?,?,?,?,?,?,?,?,?)");
			ps.setString(1, name.getText());
			ps.setString(2, shortname.getText());
			ps.setString(3, ospecname.getText());
			ps.setInt(4, Integer.parseInt(ospecstr.getText()));
			ps.setString(5, dspecname.getText());
			ps.setInt(6, Integer.parseInt(dspecstr.getText()));
			ps.setString(7, elitename.getText());
			ps.setInt(8, Integer.parseInt(eliteostr.getText()));
			ps.setInt(9, Integer.parseInt(elitedstr.getText()));
			ps.setInt(10, Integer.parseInt(eliteoutratio.getText()));
			ps.setDouble(11, Double.parseDouble(elitenw.getText()));
			ps.executeUpdate();
		 }
		 itemName = name.getText();
		 success = true;
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			rs.close();
		 } catch (final Exception e) {
		 }
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return success;
   }
}
